/**
 * Created by graysoft on 2017/2/21.
 */
/*global __dirname*/
var webpack = require("webpack");
var path = require("path");
var glob = require("glob");

var htmlWebpackPlugin = require("html-webpack-plugin");
var webpackCopyPlugin = require("copy-webpack-plugin");
var webpackDelPlugin = require("webpack-del-plugin");
var webpackNotifierPlugin = require("webpack-notifier");

var ROOT_PATH = path.resolve(__dirname);
var SRC_PATH = path.resolve(ROOT_PATH, "source");
var DIST_PATH = path.resolve(ROOT_PATH, "dist");
var TEM_PATH = path.resolve(ROOT_PATH, "templates");

var ProjectArray = glob.sync(SRC_PATH + "/*/index.js");
var ProjectEntries = {};

var config = {
  entry: {
    vendors: ["react", "react-dom", "underscore", "jquery"],
    intlVendor: ["react-intl"],
    jstreeVendor: ["jstree"],
    chartVendors: ["react-chartjs-2", "chart.js"]
  },
  output: {
    path: DIST_PATH,
    filename: "[name].[hash].js"
  },
  module: {
    rules: [
      {
        test: /\.(es6|js)$/,
        use: [
          {
            loader: "babel-loader"
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(scss|css)$/,
        use: [
          {
            loader: "style-loader"
          },
          {
            loader: "css-loader"
          },
          {
            loader: "sass-loader"
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpeg|jpg|gif)$/,
        use: [
          {
            loader: "url-loader",
          }
        ],
        exclude: /node_modules/
      },
      {
        test: /\.(woff2?|otf|eot|svg|ttf)$/,
        use: [
          {
            loader: "url-loader"
          }
        ]
      }
    ]
  },
  plugins: [
    new webpackDelPlugin({match: DIST_PATH}),
    new webpackNotifierPlugin({excludeWarnings: true}),
    new webpack.optimize.UglifyJsPlugin({minimize: true}),
    new webpack.optimize.CommonsChunkPlugin({
      name: "vendors",
      filename: "vendors.js"
    })
  ]
};

ProjectArray.forEach(function (f) {
  var regex = new RegExp(".*\/source\/(.*?)\/index\.js");
  var name = regex.exec(f)[1];
  ProjectEntries[name] = f;

  switch (name) {
  case "React_i18n": {
    config.plugins.push(
      new htmlWebpackPlugin({
        title: name,
        filename: name + ".html",
        template: path.resolve(TEM_PATH, "./index.html"),
        inject: "body",
        chunks: ["vendors", "intl", name]
      })
    );
  }
    break;
  case "JSTree": {
    config.plugins.push(
      new htmlWebpackPlugin({
        title: name,
        filename: name + ".html",
        template: path.resolve(TEM_PATH, "./index.html"),
        inject: "body",
        chunks: ["vendors", "jstreeVendor", name]
      })
    );
  }
    break;
  case "weather": {
    config.plugins.push(
      new htmlWebpackPlugin({
        title: name,
        filename: name + ".html",
        template: path.resolve(TEM_PATH, "./index.html"),
        inject: "body",
        chunks: ["vendors", "chartVendors", name]
      })
    );
  }
    break;
  default: {
    config.plugins.push(
      new htmlWebpackPlugin({
        title: name,
        filename: name + ".html",
        template: path.resolve(TEM_PATH, "./index.html"),
        inject: "body",
        chunks: ["vendors", name]
      })
    );
  }
    break;
  }


  var AssetArray = glob.sync(SRC_PATH + "/" + name + "/*/");
  AssetArray.forEach(function (f) {
    var assetRegex = new RegExp(".*\/source\/" + name + "\/(.*?)\/");
    var asset = assetRegex.exec(f)[1];

    if (asset !== "components") {
      config.plugins.push(
        new webpackCopyPlugin([
          {
            from: path.resolve(SRC_PATH, name, asset),
            to: path.resolve(DIST_PATH, name, asset)
          },
          {
            from: path.resolve(SRC_PATH, name, asset),
            to: path.resolve(DIST_PATH, name, asset)
          },
          {
            from: path.resolve(SRC_PATH, name, asset),
            to: path.resolve(DIST_PATH, name, asset)
          },
          {
            from: path.resolve(SRC_PATH, name, asset),
            to: path.resolve(DIST_PATH, name, asset)
          },
          {
            from: path.resolve(SRC_PATH, name, asset),
            to: path.resolve(DIST_PATH, name, asset)
          }
        ])
      );
    }
  });

});

config.entry = Object.assign({}, config.entry, ProjectEntries);

module.exports = config;